﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Plan
{
    public partial class PlanInfoAdd : Form
    {
        PlanForm planForm;
        string deviceTypeID;
        public string playListID;
        public PlanInfoAdd(PlanForm planForm)
        {
            InitializeComponent();
            this.planForm = planForm;
        }

        private async void CbDeviceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbDeviceType.Text)
            {
                case "交通信号灯":
                    {
                        string sql = "SELECT DeviceID, Name FROM M_Device WHERE (TypeID = 'TS')";
                        DBHelper sdh = new DBHelper();
                        DataTable dtts = await sdh.GetTableAsync(sql);
                        cbDevice.ValueMember = "DeviceID";
                        cbDevice.DisplayMember = "Name";
                        cbDevice.DataSource = dtts;
                        cbPlanInfo.DataSource = null;
                        cbPlanInfo.Items.Clear();
                        cbPlanInfo.Items.Add("红灯");
                        cbPlanInfo.Items.Add("黄灯");
                        cbPlanInfo.Items.Add("绿灯");
                        deviceTypeID = "TS";
                    }
                    break;
                case "风机":
                    {
                        string sql = "SELECT DeviceID, Name FROM M_Device WHERE (TypeID = 'FAN')";
                        DBHelper sdh = new DBHelper();
                        DataTable dtts = await sdh.GetTableAsync(sql);
                        cbDevice.ValueMember = "DeviceID";
                        cbDevice.DisplayMember = "Name";
                        cbDevice.DataSource = dtts;
                        cbPlanInfo.DataSource = null;
                        cbPlanInfo.Items.Clear();
                        cbPlanInfo.Items.Add("正转");
                        cbPlanInfo.Items.Add("反转");
                        cbPlanInfo.Items.Add("停止");
                        deviceTypeID = "FAN";
                    }
                    break;
                case "照明":
                    {
                        string sql = "SELECT TypeID as DeviceID, Name FROM M_Device WHERE (DevicePos = 'Light') GROUP BY TypeID,Name";
                        DBHelper sdh = new DBHelper();
                        DataTable dtts = await sdh.GetTableAsync(sql);
                        cbDevice.ValueMember = "DeviceID";
                        cbDevice.DisplayMember = "Name";
                        cbPlanInfo.DataSource = null;
                        cbDevice.DataSource = dtts;
                        cbPlanInfo.Items.Clear();
                        cbPlanInfo.Items.Add("点亮");
                        cbPlanInfo.Items.Add("熄灭");
                        deviceTypeID = cbDevice.SelectedValue.ToString();
                    }
                    break;
                case "可变情报板":
                    {
                        string sql = "SELECT DeviceID, Name FROM M_Device WHERE (TypeID = 'FCMS')";
                        DBHelper sdh = new DBHelper();
                        DataTable dtts = await sdh.GetTableAsync(sql);
                        cbDevice.ValueMember = "DeviceID";
                        cbDevice.DisplayMember = "Name";
                        cbDevice.DataSource = dtts;
                        string sql2 = "SELECT * FROM M_PlayList";
                        DBHelper dh = new DBHelper();
                        DataTable dtpl = await dh.GetTableAsync(sql2);
                        cbPlanInfo.ValueMember = "PlayListID";
                        cbPlanInfo.DisplayMember = "Name";
                        cbPlanInfo.DataSource = dtpl;
                        deviceTypeID = "FCMS";
                    }
                    break;
            }
        }
        private bool CheckTheSameDevice()
        {
            var result = from s1 in planForm.dataGridView1.Rows.Cast<DataGridViewRow>()
                         where s1.Cells["设备ID"].Value.ToString() == cbDevice.SelectedValue.ToString()
                         select s1;
            if (result.Count() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void CbPlanInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPlanInfo.SelectedValue != null)
            {
                playListID = cbPlanInfo.SelectedValue.ToString();
                lbPlayListID.Text = playListID;
            }
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (cbDevice.SelectedItem != null && cbPlanInfo.SelectedItem != null)
            {
                if (CheckTheSameDevice())
                {
                    MessageBox.Show("请勿在同一预案中重复添加相同设备", "重复添加设备警告");
                }
                else
                {
                    planForm.dataGridView1.Rows.Add(1);
                    DataGridViewRow lastDr = planForm.dataGridView1.Rows[planForm.dataGridView1.RowCount - 1];
                    lastDr.Cells["设备名称"].Value = cbDevice.Text;
                    lastDr.Cells["设备状态"].Value = cbPlanInfo.Text;
                    lastDr.Cells["设备ID"].Value = cbDevice.SelectedValue.ToString();
                    lastDr.Cells["设备类型ID"].Value = deviceTypeID;
                    lastDr.Cells["播放列表ID"].Value = playListID;
                }
            }
        }

        private void CbDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbDeviceType.Text)
            {
                case "交通信号灯":
                    {
                        deviceTypeID = "TS";
                    }
                    break;
                case "风机":
                    {
                        deviceTypeID = "FAN";
                    }
                    break;
                case "照明":
                    {
                        deviceTypeID = cbDevice.SelectedValue.ToString();
                    }
                    break;
                case "可变情报板":
                    {
                        deviceTypeID = "FCMS";
                    }
                    break;
            }
        }
    }
}
